package org.club.mapper;

import org.apache.ibatis.annotations.*;
import org.club.common.model.dto.ActivityQueryDTO;
import org.club.common.model.dto.AnnouncementQueryDTO;
import org.club.common.model.dto.ClubsDto;
import org.club.common.model.entity.AnnouncementWithReadStatus;
import org.club.common.model.entity.ClubActivityInfo;
import org.club.common.model.entity.Clubs;
import org.club.common.model.vo.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //查询社团
    List<ClubInfoVO> selectClubsList(@Param("status") Integer status, @Param("name") String name,@Param("userId") Long userId);

    //查询社团总数
    @Select("SELECT COUNT(*) FROM clubs")
    Integer selectClubsListTotal();

    //查询用户是否存在users表里面
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int selectUser(@Param("username") String username);

    //查询用户是否存在club_members表里面
    @Select("SELECT COUNT(*) FROM club_members WHERE user_id = #{userId} AND club_id = #{clubId} AND status=0")
    int selectClubsUser(@Param("clubId") Long clubId, @Param("userId") Long userId);

    //查询用户是否存在club_admins表里面
    @Select("SELECT COUNT(*) FROM club_admins WHERE user_id = #{username}")
    int selectclub_admins(@Param("username") String username);

    //加入社团
    Integer joinClub(@Param("clubId") Long clubId, @Param("userId") Long userId);

    //查询用户id
    @Select("SELECT id FROM users WHERE username = #{username}")
    Long selectUserId(@Param("username") String username);

    //查询用户加入的社团id
    @Select("SELECT club_id FROM club_members WHERE user_id = #{userId} AND status = 0")
    List<Long> selectClubsIdLsit(Long userId);
//查询用户加入的社团
    List<ClubInfoVO> selectjoinClubsList(@Param("clubIdList") List<Long> clubIdList);
//退出社团
    @Delete("DELETE FROM club_members WHERE club_id = #{clubId} AND user_id = #{userId}")
    Integer quitClub(@Param("clubId") Long clubId,@Param("userId") Long userId);
//查询社团已经发布的活动
    List<ActivitiesVO> selectActivities(@Param("clubId") Long clubId);
//报名活动
    Integer signUpActivity( @Param("activityId")Long  activityId,@Param("userId") Long userId);
//查询活动列表
    List<ActivitiesNumberApplicantsVO> selectActivitiesList(ActivityQueryDTO activityQueryDTO);
    //查询用户报名的活动
    List<Registration_InformationVO> selectUserSignUpActivities(@Param("userId") Long userId, @Param("status") Integer status, @Param("keyword") String keyword);
    //    创建社团
    Integer CreateCommunity(ClubsDto clubsDto);
    //    插入社团管理员
    Integer Insertionclub_admins(@Param("clubId") Long id,@Param("userId") Long userId);
//统计查询用户在club_members的所有记录
    @Select("SELECT COUNT(*) FROM club_members WHERE user_id = #{userId} AND status=0")
    Integer selectclub_members(Long userId);
//查询系统公告
    List<AnnouncementWithReadStatus> selectSystemAnnouncements(AnnouncementQueryDTO announcementQueryDTO);
//显示未读数量的接口
    @Select("SELECT COUNT(*) FROM announcement_users WHERE user_id=#{userId} AND read_status=0")
    Integer selectUnreadCount(@Param("userId") Long userId);
//修改公告已读状态
    @Update("UPDATE announcement_users SET read_status = 1 WHERE announcement_id = #{announcement_id}")
    Integer updateAnnouncementReadStatus(@Param("announcement_id") Long announcement_id);
//查询用户提交的创建社团审核信息
    Clubs selectClubs(@Param("userId") Long userId);

//查询该用户是否已经参加了该活动
    @Select("SELECT COUNT(*) AS count\n" +
            "FROM activity_registrations\n" +
            "WHERE user_id = #{userId}\n" +
            "  AND activity_id = #{activityId};")
    int selectUserSignUpActivitiesCount(@Param("userId") Long userId,@Param("activityId") Long activityId);
//查询用户加入的社团状态
    @Select("SELECT COUNT(*) FROM club_members WHERE user_id = #{userId} AND club_id = #{clubId}")
    Integer selectClubsUserStatus(@Param("userId") Long userId,@Param("clubId") Long clubId);
//更新用户加入的社团状态
    @Update("UPDATE club_members SET status = 0 WHERE user_id = #{userId} AND club_id = #{clubId}")
    Integer UpdateClubsUsersttus(@Param("clubId") Long clubId,@Param("userId") Long userId);
//查询社团详情
    @Select("SELECT * FROM club_details WHERE club_id = #{clubId}")
    ClubsDetailVO selectClubsDetail(@Param("clubId") Long clubId);
//查询社团成员和活动
    List<ClubActivityInfo> selectClubsMembers(@Param("clubId") Long clubId);
}

